﻿@using System.Linq
@using StackExchange.Opserver.Data.CloudFlare
@using StackExchange.Opserver.Views.CloudFlare
@model DNSModel
@{
    var records = Model.DNSRecords;
    var zones = Model.Zones;
    var dcs = Model.DataCenters;
    var showDCs = dcs.Any();
    var hiddenRecordTypes = new List<DNSRecordType> {DNSRecordType.TXT};
}
<style>
     .cloudflare-tabular table {
         width: 1024px;
         margin: 10px auto;
     }
</style>
<div class="cloudflare-tabular">
    <table class="striped-dashboard">
        <thead>
            <tr>
                <th colspan="@(@dcs.Count + 3)">
                    <div>
                        <h3>
                            CloudFlare DNS
                            @Helpers.PollNow(CloudFlareAPI.Instance)
                        </h3>
                    </div>
                </th>
            </tr>
            <tr>
                <th>Zone</th>
                <th>DNS Servers</th>
                @if (showDCs)
                {
                    foreach (var dc in dcs)
                    {
                        <th>@dc.Key</th>
                    }
                }
                <th>Total</th>
            </tr>
        </thead>
        <tbody>
            @foreach (var z in zones)
            {
                <tr>
                    <td>@(z.MonitorStatus.IconSpan()) <b>@z.Name</b></td>
                    <td>
                        @if (z.VanityNameServers != null && z.VanityNameServers.Count > 0)
                        {
                            @:@string.Join(", ", z.VanityNameServers)
                        }
                        else if (z.NameServers != null)
                        {
                            @:@string.Join(", ", z.NameServers)
                        }
                    </td>
                    @if (showDCs)
                    {
                        foreach (var dc in dcs)
                        {
                            var count = z.DNSRecords.Where(r => r.IPAddress != IPAddress.None).Count(r => dc.Value.Any(ipnet => ipnet.Contains(r.IPAddress)));
                            <td>@count.ToComma()</td>
                        }
                    }
                    <td>@z.DNSRecords.Count</td>
                </tr>
            }
        </tbody>
    </table>

    <table class="striped-dashboard">
        <thead>
            <tr>
                <th>Type</th>
                <th>Name</th>
                <th>Value</th>
                <th>TTL</th>
                <th title="Is this domain being provied through CloudFlare?">CF</th>
                <th>Data Centers</th>
            </tr>
        </thead>
        <tbody>
            @foreach (var r in records.OrderBy(r => r.Type).ThenBy(r => r.ZoneName).ThenBy(r => r.Name))
            {
                if (hiddenRecordTypes.Contains(r.Type))
                {
                    continue;
                }
                var rootIPs = r.RootIPAddresses;
                var servedDCs = rootIPs != null ? dcs.Where(dc => dc.Value.Any(range => rootIPs.Any(range.Contains))).ToList() : null;
                var masked = rootIPs != null ? DataCenters.GetMasked(rootIPs).ToList() : null;
                var recordDCs = servedDCs != null && servedDCs.Any() ? string.Join(", ", servedDCs.Select(dc => dc.Key)) : "Other";
                <tr>
                    <td class="@(r.Type == DNSRecordType.CNAME ? "warning" : "bold")">@r.Type</td>
                    <td><b>@(r.Name != r.ZoneName ? r.Name.Replace(r.ZoneName, "") : r.Name)</b>@if (r.Name != r.ZoneName){<span class="note">.@r.ZoneName</span>}</td>
                    <td>
                        @switch (r.Type)
                        {
                            case DNSRecordType.CNAME:
                                @r.Content
                                if (rootIPs != null && rootIPs.Any())
                                {
                                    <span class="note">(@string.Join(", ", masked))</span>
                                }
                                break;
                            default:
                                @(masked != null ? string.Join(", ", masked) : null)
                                break;
                        }
                    </td>
                    <td>@(r.IsAutoTTL ? "Auto" : $"{r.TTL}s")</td>
                    <td>@(r.Proxied ? MonitorStatus.Good.IconSpan() : MonitorStatus.Unknown.IconSpan())</td>
                    <td class="@(r.Proxied ? "all-good" : "")">
                        @recordDCs
                    </td>
                </tr>
            }
        </tbody>
    </table>
    <script>
        $(function() {
            $('.striped-dashboard').tablesorter();
        });
    </script>
</div>